﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;

using SixLottery.Helpers;
using SixLottery.Core;
using SixLottery.Common;
using SixLottery.Data;

namespace SixLottery.Models
{
    public class Article : Entity
    {
        public string Title { get; set; }
        
        public string Content { get; set; }

        public int CreatedBy { get; set; }

        public DateTime CreatedDate { get; set; }
        
        public bool IsDelete { get; set; }
        
        public int Id { get; set; }

        public int UpdatedBy { get; set; }

        public DateTime UpdatedDate { get; set; }

        public Article()
        {

        }

        [DarrenAjax.Utility.jQueryInvokeMethod(Url = "handlers/AjaxController.ashx", Method = "post", DataType = "json", Async = true)]
        public static ResultMessage Create(Article article)
        {
            string sql = "insert into article (title,content,createdDate,isdeleted) values (@title, @content, @createdDate, @isDeleted)";

            Dictionary<string, object> args = new Dictionary<string, object>();
            args.Add("title", article.Title);
            args.Add("content", article.Content);
            args.Add("createdDate", DateTime.Now.ToString());
            args.Add("isDeleted", 0);

            try
            {
                Access.GetInstance(Config.ConnectionString).Execute(sql, args);
                return new ResultMessage() { ErrorCode = 0 };
            }
            catch (Exception ex)
            {
                return new ResultMessage() { ErrorCode = 1, Description = ex.Message + "." + sql };
            }
        }

        [DarrenAjax.Utility.jQueryInvokeMethod(Url = "handlers/AjaxController.ashx", Method = "post", DataType = "json", Async = false)]
        public static ResultMessage GetDetail(int articleId)
        {
            string sql = "select * from article where id = @articleId";
            Dictionary<string, object> args = new Dictionary<string, object>();

            args.Add("articleId", articleId);
            DataTable dt = Access.GetInstance(Config.ConnectionString).GetDataTable(sql, args);
            
            if (dt.Rows.Count > 0)
            {
                Article article = new Article();
                article.Load(dt.Rows[0]);
                
                return new ResultMessage() {
                    ErrorCode = 1,
                    Value = article
                };
            }
            else
            {
                return new ResultMessage() { ErrorCode = 0, Description = "没有数据" };
            }
        }

        [DarrenAjax.Utility.jQueryInvokeMethod(Url = "handlers/AjaxController.ashx", Method = "post", DataType = "json", Async = false)]
        public static ResultMessage GetPaging(int pagesize, int pagenum)
        {
            HttpRequest request = HttpContext.Current.Request;
            if (request["pagesize"] != null)
                pagesize = int.Parse(request["pagesize"]);
            if (request["pagenum"] != null)
                pagenum = int.Parse(request["pagenum"]);

            var memberList = GetAllArticle();
            var ps = memberList.Select(m => new { Title = m.Title, Message = m.Content, CreateDate = m.CreatedDate, ID = m.Id }).Skip((pagenum - 1) * pagesize).Take(pagesize); ;
            var data = new { total = memberList.Count, rows = ps };
            return new ResultMessage() { ErrorCode = 0, Name = "success", Value = data };
        }

        [DarrenAjax.Utility.jQueryInvokeMethod(Url = "handlers/AjaxController.ashx", Method = "post", DataType = "json", Async = true)]
        public static ResultMessage Delete(string articleId)
        {
            string sql = "update [Article] set [isdeleted]=1 where [ID] = @articleId";

            Dictionary<string, object> args = new Dictionary<string, object>();
            args.Add("articleId", articleId);

            Access.GetInstance(Config.ConnectionString).Execute(sql, args);            

            return new ResultMessage() { ErrorCode = 0 };
        }
        public static IList<Article> GetAllArticle()
        {            
            string sql = "select * from article where isdeleted=0 order by updatedDate desc";
            Dictionary<string, object> args = new Dictionary<string,object>();

            DataTable dt = Access.GetInstance(Config.ConnectionString).GetDataTable(sql, args);

            EntityList<Article> articleList = new EntityList<Article>();
            articleList.Load(dt);

            return articleList;
        }

        [DarrenAjax.Utility.jQueryInvokeMethod(Url = "handlers/AjaxController.ashx", Method = "post", DataType = "json", Async = true)]
        public static ResultMessage Update(Article article)
        {
            string sql = "update article set title=@title, content=@content where id = @articleId";

            Dictionary<string, object> args = new Dictionary<string, object>();
            args.Add("title", article.Title);
            args.Add("content", article.Content);
            args.Add("articleId", article.Id);

            Access.GetInstance(Config.ConnectionString).Execute(sql, args);

            return new ResultMessage() { ErrorCode = 0 };
        }
    }
}